package com.example.mybaseapp

import androidx.appcompat.widget.AppCompatTextView
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.example.mybaseapp.ui.FirstActivity
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        assertEquals("com.example.mybaseapp", appContext.packageName)
    }

    @get:Rule
    val activityScenarioRule = ActivityScenarioRule(FirstActivity::class.java)

    @Test
    fun t1(){

        ActivityScenario.launch(FirstActivity::class.java).use { scenario ->
            scenario.onActivity { activity ->
                val textView = activity.findViewById<AppCompatTextView>(R.id.tx_content)
                textView.text = "Expected Text Here Expected Text Here Expected Text Here Expected Text Here"

//                onView(withId(R.id.tx_content))
//                    .perform(scrollTo()) // 模拟滚动以确保TextView内容可见
//                    .check(matches(withText("Expected Text Here"))) // 验证显示内容与预期内容一致
            }}

    }

    @Test
    fun  t3(){
        val context = InstrumentationRegistry.getInstrumentation().targetContext
        ActivityScenario.launch(FirstActivity::class.java).use { scenario ->
            scenario.onActivity { activity ->
                val textView = activity.findViewById<AppCompatTextView>(R.id.tx_content)
                textView.text = "Expected Text Here Expected Text Here Expected Text Here Expected Text Here"
                MainScope().launch(IO){
                    onView(withId(R.id.tx_content)).check(matches(hasDescendant(withText("Expected Text Here Expected Text Here Expected Text Here Expected Text Here"))))
                }

//                val layout = textView.layout
//
//// 获取当前可见的最后一行的行数
//                val lastVisibleLine = layout.getLineForVertical(textView.scrollY + textView.height)
//
//// 获取当前可见的最后一行的起始和结束位置
//                val startOffset = layout.getLineStart(lastVisibleLine)
//                var endOffset = layout.getLineEnd(lastVisibleLine)
//
//                if (layout.getEllipsisCount(lastVisibleLine) > 0) {
//                    // 如果行末出现省略号，则调整结束位置
//                    endOffset = layout.getEllipsisStart(lastVisibleLine)
//                }
//
//// 获取当前显示内容的结束位置
//                val visibleTextEnd = textView.text.substring(startOffset, endOffset)
//                Log.d("Fire", "t: $visibleTextEnd")
//                println("visibleTextEnd:$visibleTextEnd")
            }

        }

    }
}